<?php
/**
 * nutzp boot script
 */
if (ini_get ( 'register_globals' )) {
	exit ( "Error: register_globals must be set to Off in /etc/php.ini" );
}
define ( 'LDEBUG', 4 ); // 调试级别
define ( 'LWARN', 3 ); // 警告级别
define ( 'LINFO', 2 ); // 信息级别
define ( 'LERROR', 1 ); // 错误级别
define ( 'LOFF', 0 ); // 关闭调试信息
define ( 'NOTNULL', '_NUTZP_ORM_NOT_NULL_VALUE__' );
/**
 * 新建目录权限
 */
define ( 'FS_CHMOD_DIR', 0755 );
/**
 * 新建文件权限
 */
define ( 'FS_CHMOD_FILE', 0644 );
/**
 * 连接文件系统超时
 */
define ( 'FS_TIMEOUT', 30 );
define ( 'FS_CONNECT_TIMEOUT', 30 );

if (! defined ( 'ROOT' )) {
	define ( 'ROOT', dirname ( dirname ( __FILE__ ) ) . DS . 'webapp' );
}

if (! defined ( 'WWW_ROOT' )) {
	define ( 'WWW_ROOT', ROOT . DS . WEBROOT_DIR . DS );
}

if (! defined ( 'APP_NAME' )) {
	define ( 'APP_NAME', basename ( ROOT ) );
}

if (function_exists ( 'ini_set' )) {
	ini_set ( 'include_path', ini_get ( 'include_path' ) . PATH_SEPARATOR . ROOT );
}

defined ( 'APP_PATH' ) or define ( 'APP_PATH', ROOT . DS );

require APP_PATH . 'config.php'; // load application configuration


defined ( 'LOG_LEVEL' ) or define ( "LOG_LEVEL", LINFO ); //


if (! defined ( 'DEBUG' )) {
	define ( 'DEBUG', LOG_LEVEL > 0 );
}
if (LOG_LEVEL > 2) {
	define ( 'DEBUG_DISPLAY', true );
}
/**
 * #@-
 */
/**
 * 系统类路径
 *
 * @global array $__exports
 * @name $__exports
 */
$__exports = array ();
/**
 * 系统用户信息
 *
 * @global array $__whoami
 * @name $__whoami
 */
$__whoami = array ();
/**
 * 系统当前 SESSION 实例
 *
 * @global ISession $__sess
 * @name $__sess
 * @var ISession
 */
$__sess = null;
/**
 * 系统当前邮件发送器实例
 *
 * @global IMailer $__mailer
 * @name $__mailer
 * @var IMailer
 */
$__mailer = null;
/**
 * 系统当前 CACHE 实例
 *
 * @global ICache $__cache
 * @name $__cache
 * @var ICache
 */
$__cache = null;
/**
 * 系统请求实例
 *
 * @global Request $__rqst
 * @name $__rqst
 * @var Request
 */
$__rqst = null;
/**
 * 访问当前页面的URL，可用于分页。
 *
 * @global string $__url
 * @name $__url
 * @var string
 */
$__url = '';
/**
 * smarty模板引擎。
 *
 * @global Smarty $__smarty
 * @name $__smarty
 * @var Smarty
 */
$__smarty = null;
/**
 * 
 * 模板全局变更
 * @global $__global
 * @name $__global
 * @var array
 */
$__global = array ();
/**
 * 
 * 当前分页
 * @global int $__cpage
 * @name $__cpage
 * @var int
 */
$__cpage = 1;
/**
 * rbac权限控制实例
 *
 * @global IRbac $__rbac
 * @name $__rbac
 * @var IRbac
 */
$__rbac = null;

if (defined ( 'DEBUG' ) && DEBUG) {
	if (defined ( 'E_DEPRECATED' )) {
		error_reporting ( E_ALL & ~ E_DEPRECATED & ~ E_STRICT & ~ E_NOTICE );
	} else {
		error_reporting ( E_ALL & ~ E_NOTICE );
	}
	if (defined ( 'DEBUG_DISPLAY' ) && DEBUG_DISPLAY) {
		@ini_set ( 'display_errors', 1 );
	} else {
		@ini_set ( 'display_errors', 0 );
	}
	if (defined ( 'DEBUG_LOG' ) && DEBUG_LOG) {
		@ini_set ( 'log_errors', 1 );
		@ini_set ( 'error_log', TMP . 'debug.log' );
	}
} else {
	if (! defined ( 'DEBUG' )) {
		/**
		 * #@+
		 * Constants
		 */
		/**
		 * 调试开关
		 *
		 * 如果在{@link config.php}中未定义,此处关闭调试
		 *
		 * @var boolean
		 */
		define ( 'DEBUG', false );
	/**
	 * #@-
	 */
	}
	if (defined ( 'E_RECOVERABLE_ERROR' )) {
		error_reporting ( E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR );
	} else {
		error_reporting ( E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING );
	}
}
/**
 * #@-
 */
if (version_compare ( phpversion (), '5.3', '<' )) {
	@set_magic_quotes_runtime ( 0 );
}
@ini_set ( 'magic_quotes_sybase', 0 );
// 浏览器检测
if (! defined ( 'RUNTIME_MEMORY_LIMIT' )) {
	/**
	 * #@+
	 * Constants
	 */
	/**
	 * 运行时内存限制
	 *
	 * @var string
	 */
	define ( 'RUNTIME_MEMORY_LIMIT', '128M' );
}
if (function_exists ( 'memory_get_usage' ) && (( int ) @ini_get ( 'memory_limit' ) < abs ( intval ( RUNTIME_MEMORY_LIMIT ) ))) {
	@ini_set ( 'memory_limit', RUNTIME_MEMORY_LIMIT );
}
if (function_exists ( 'mb_internal_encoding' )) {
	mb_internal_encoding ( 'UTF-8' );
}
/**
 * 取消全局变量的注册
 *
 * @access private
 */
function __unregister_GLOBALS() {
	if (! ini_get ( 'register_globals' )) {
		return;
	}
	if (isset ( $_REQUEST ['GLOBALS'] )) {
		die ( 'GLOBALS overwrite attempt detected' );
	}
	$noUnset = array ('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', '_KCF', 'cfg_opts' );
	$input = array_merge ( $_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset ( $_SESSION ) && is_array ( $_SESSION ) ? $_SESSION : array () );
	foreach ( $input as $k => $v ) {
		if (! in_array ( $k, $noUnset ) && isset ( $GLOBALS [$k] )) {
			$GLOBALS [$k] = NULL;
			unset ( $GLOBALS [$k] );
		}
	}
}
__unregister_GLOBALS ();
// Fix for IIS when running with PHP ISAPI
if (empty ( $_SERVER ['REQUEST_URI'] ) || (php_sapi_name () != 'cgi-fcgi' && preg_match ( '/^Microsoft-IIS\//', $_SERVER ['SERVER_SOFTWARE'] ))) {
	
	// IIS Mod-Rewrite
	if (isset ( $_SERVER ['HTTP_X_ORIGINAL_URL'] )) {
		$_SERVER ['REQUEST_URI'] = $_SERVER ['HTTP_X_ORIGINAL_URL'];
	} else if (isset ( $_SERVER ['HTTP_X_REWRITE_URL'] )) { // IIS Isapi_Rewrite
		$_SERVER ['REQUEST_URI'] = $_SERVER ['HTTP_X_REWRITE_URL'];
	} else {
		// Use ORIG_PATH_INFO if there is no PATH_INFO
		if (! isset ( $_SERVER ['PATH_INFO'] ) && isset ( $_SERVER ['ORIG_PATH_INFO'] )) {
			$_SERVER ['PATH_INFO'] = $_SERVER ['ORIG_PATH_INFO'];
		}
		// Some IIS + PHP configurations puts the script-name in the path-info
		// (No need to append it twice)
		if (isset ( $_SERVER ['PATH_INFO'] )) {
			if ($_SERVER ['PATH_INFO'] == $_SERVER ['SCRIPT_NAME'])
				$_SERVER ['REQUEST_URI'] = $_SERVER ['PATH_INFO'];
			else
				$_SERVER ['REQUEST_URI'] = $_SERVER ['SCRIPT_NAME'] . $_SERVER ['PATH_INFO'];
		}
		
		// Append the query string if it exists and isn't null
		if (isset ( $_SERVER ['QUERY_STRING'] ) && ! empty ( $_SERVER ['QUERY_STRING'] )) {
			$_SERVER ['REQUEST_URI'] .= '?' . $_SERVER ['QUERY_STRING'];
		}
	}
}

// Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in
// php.cgi for all requests
if (isset ( $_SERVER ['SCRIPT_FILENAME'] ) && (strpos ( $_SERVER ['SCRIPT_FILENAME'], 'php.cgi' ) == strlen ( $_SERVER ['SCRIPT_FILENAME'] ) - 7)) {
	$_SERVER ['SCRIPT_FILENAME'] = $_SERVER ['PATH_TRANSLATED'];
}
// Fix for Dreamhost and other PHP as CGI hosts
if (strpos ( $_SERVER ['SCRIPT_NAME'], 'php.cgi' ) !== false) {
	unset ( $_SERVER ['PATH_INFO'] );
}
// Fix empty PHP_SELF
$PHP_SELF = $_SERVER ['PHP_SELF'];
if (empty ( $PHP_SELF )) {
	$_SERVER ['PHP_SELF'] = $PHP_SELF = preg_replace ( '/(\?.*)?$/', '', $_SERVER ["REQUEST_URI"] );
}
if (version_compare ( '5.2', phpversion (), '>' )) {
	die ( sprintf ( 'Your php verion is %s,but kickass reqired  php 5.2+', phpversion () ) );
}
require 'version.php'; // version defination file
require 'paths.php'; // global pathes for the application
require 'i18n.php';
require 'functions.php';
require 'formatting.php';
require 'plugin.php';
require 'template.php';
require NUTZP_PATH . 'core' . DS . 'Passport.php';
// //////////////////////////////////////////////////
// 自动加载器
/**
 * 自动类加载器
 *
 * 根据类名和已经注册到系统的类路径{@link $__exports}动态加载类
 *
 * @global array 系统类路径
 * @param $clz string
 * 类名
 */
function __nutzp_class_loader($clz) {
	global $__exports;
	if (! empty ( $__exports )) {
		foreach ( $__exports as $p ) {
			$f = $p . DS . $clz . '.php';
			if (file_exists ( $f )) {
				include $f;
				return;
			}
			$f = $p . DS . $clz . '.class.php';
			if (file_exists ( $f )) {
				include $f;
				return;
			}
		}
	}
	if (is_dir ( APP_PATH . 'classes' )) {
		$appclasses = find_files ( APP_PATH . 'classes', '/^' . $clz . '\.php$/', null, true );
		if (! empty ( $appclasses )) {
			include $appclasses [0];
			return;
		}
	}
	if (is_dir ( APP_PATH . 'models' )) {
		$appclasses = find_files ( APP_PATH . 'models', '/^' . $clz . '\.php$/', null, true );
		if (! empty ( $appclasses )) {
			include $appclasses [0];
			return;
		}
	}
	$appclasses = find_files ( MODULE_PATH, '/^' . $clz . '\.php$/', array ('views', 'controllers' ), true );
	if (! empty ( $appclasses )) {
		include $appclasses [0];
		return;
	}	
	$file = apply_filter ( 'auto_class_load', '', $clz );
	if ($file && file_exists ( $file )) {
		include $file;
	}
}
/**
 * 加载系统插件
 *
 * @uses load_application_plugins 应用程序自定义的插件加载方法
 */
function __nutzp_load_plugins() {
	// 这儿给应用程序一个自定义加载插件的方法，默认不加载用户插件
	if (function_exists ( 'load_application_plugins' )) {
		load_application_plugins ();
	}
	include CORE_PLUGIN_PATH . 'session_plugin.php';
	include CORE_PLUGIN_PATH . 'datasource_plugin.php';
}
spl_autoload_register ( '__nutzp_class_loader' );
if (file_exists ( APP_PATH . 'routes.php' )) {
	require APP_PATH . 'routes.php'; // load url route
}
if (file_exists ( APP_PATH . 'autoload.php' )) {
	require APP_PATH . 'autoload.php'; // auto load script
}
__nutzp_load_plugins (); // load plugins
defined ( 'TIMEZONE' ) or define ( 'TIMEZONE', apply_filter ( 'get_app_timezone', 'Asia/Shanghai' ) );
@date_default_timezone_set ( TIMEZONE );
if (! defined ( 'BASE_URL' )) {
	define ( 'BASE_URL', trailingslashit ( apply_filter ( 'get_base_url', '/' ) ) );
}
defined ( 'ADMIN_DIR' ) or define ( 'ADMIN_DIR', 'admin' );
define ( 'ADMIN_URL', BASE_URL . ADMIN_DIR . '/' );
define ( 'ADDIN_PATH', WWW_ROOT . ADMIN_DIR . DS );
define ( 'ADMIN_LOGIN_PAGE', ADMIN_URL . 'login.php' );
defined ( 'COOKIE_KEY' ) or define ( 'COOKIE_KEY', apply_filter ( 'get_cookie_key', 'yeN3gX6bNfiaYfodV63dI1j8Fbk5HaL7W4yaW6Gu2j4MfDESmfg2v899g451k576' ) );
fire ( "kernel_started" );
//end of bootstrap.php